import { IsDefined, IsInt, IsNotEmpty, IsString, Length } from "class-validator";
import { BaseEntity } from "@/common/BaseEntity";
import { TStatus } from "@/common/CommonType";
import { Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
import SysUser from "../user/Entity";
import SysMenu from "../menu/Entity";

@Entity({
  name: "sys_role",
  orderBy: { createTime: "DESC" },
})
export default class SysRoleEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id: number;

  @Length(2, 20)
  @IsString()
  @IsNotEmpty()
  @IsDefined()
  @Column({
    nullable: true,
  })
  public name: string;

  @IsString()
  @Column({
    nullable: true,
  })
  public roleKey: string;

  @IsString()
  @Column({
    nullable: true,
    type: "text",
  })
  rolePermissionList: string;

  @IsInt()
  @IsNotEmpty()
  @IsDefined()
  @Column({
    nullable: true,
  })
  public status: TStatus;

  @IsString()
  @Column({
    nullable: true,
  })
  public remark: string;

  /**
   * 与用户表进行关联
   */
  @ManyToMany(() => SysUser, (role) => role.roles)
  users: SysUser[];

  /**
   * 与菜单表进行关联
   */
  @ManyToMany(() => SysMenu, (role) => role.roles)
  @JoinTable()
  menus: SysMenu[] | number[];

  public static transform(plainObject: object): SysRoleEntity {
    return super.baseTransform(SysRoleEntity, plainObject);
  }
}
